//
//  HBTabBarController.m
//  XFDCommon_Example
//
//  Created by 韩冰 on 2017/11/2.
//  Copyright © 2017年 北京国泰北斗科技有限公司. All rights reserved.
//

#import "HBTabBarController.h"
#import "HBNavViewController.h"
#import "HBBaseController.h"
#import "HBCommon.h"
#define TabBarSelectBuleColor [UIColor HB_ColorWithHexString:@"#3eabf9"] //登陆蓝色
#define TabBarNormalColor [UIColor HB_ColorWithHexString:@"#666666"] //
#define SINGLE_LINE_WIDTH           (1.0 / [UIScreen mainScreen].scale)
#define SINGLE_LINE_ADJUST_OFFSET   ((1.0 / [UIScreen mainScreen].scale) / 2)
  
@interface HBTabBarController ()<UITabBarControllerDelegate>

@property (nonatomic, assign) NSInteger wantSelectIndex;
@property (nonatomic, strong)NSArray *ctrs;
@property (nonatomic, strong)NSArray *selectImages;
@property (nonatomic, strong)NSArray *normalImages;
@property (nonatomic, strong)NSArray *titles;

@end

@implementation HBTabBarController

- (instancetype)initWithControllers:(NSArray *)ctrs withSelectImages:(NSArray *)selectImages withNormalImages:(NSArray *)normalImages withTitles:(NSArray *)titles{
    
    self = [super init];
    NSLog(@"%@",self);
    if (self) {
        self.ctrs = ctrs;
        self.selectImages = selectImages;
        self.normalImages = normalImages;
        self.titles = titles;
        for (int i = 0; i< self.ctrs.count; i ++) {
            [self addChildVc:[self.ctrs objectAtIndexSafely:i] title:[self.titles objectAtIndexSafely:i] image:[self.normalImages objectAtIndexSafely:i] selectedImage:[self.selectImages objectAtIndexSafely:i]];
        }
        self.delegate = self;
        self.selectedIndex = self.idex;
        
        [[UITabBar appearance] setBackgroundImage:[self imageWithColor:[UIColor whiteColor]size:CGSizeMake(1, 1)]];
        
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0.0, SINGLE_LINE_ADJUST_OFFSET, [UIScreen mainScreen].bounds.size.width, SINGLE_LINE_WIDTH)];
        [self.tabBar addSubview:bgView];
    }
    return self;
}



- (void)viewDidLoad {
    [super viewDidLoad];
}


/**
 *  设置子控制器
 *
 *  @param childVc       子控制器
 *  @param title         标题
 *  @param image         正常图片
 *  @param selectedImage 选中时的图片
 */
- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(UIImage *)image selectedImage:(UIImage *)selectedImage
{
    [childVc.tabBarItem setTitleTextAttributes:@{ NSForegroundColorAttributeName : TabBarSelectBuleColor,NSFontAttributeName:[UIFont systemFontOfSize:11]} forState:UIControlStateSelected];
    [childVc.tabBarItem setTitleTextAttributes:@{ NSForegroundColorAttributeName : TabBarNormalColor,NSFontAttributeName:[UIFont systemFontOfSize:11]} forState:UIControlStateNormal];
    [childVc.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -3)];
    // 设置子控制器的图片
    childVc.tabBarItem.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVc.tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 给控制器包装一个导航控制器
    HBNavViewController*nav = [[HBNavViewController alloc] initWithRootViewController:childVc];
    // 添加子控制器
    [childVc.tabBarItem setTitle:title];
    [self addChildViewController:nav];
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    [tabBar.items enumerateObjectsUsingBlock:^(UITabBarItem *_Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {
        if (item == obj) {
            self.wantSelectIndex = idx;
        }
    }];
}

//- (UIImage *)imageFromImage:(UIImage *)image{
//    NSInteger size = 25;
//    UIGraphicsBeginImageContext(CGSizeMake(size, size));
//    [image drawInRect:CGRectMake(0.0f, 0.0f, size, size)];
//    image = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//    return image;
//}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    return YES;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    
}

- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
    if (!color || size.width <= 0 || size.height <= 0) return nil;
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

@end
